package org.sippr.controller;

import org.sippr.pojo.Result;
import org.sippr.pojo.SdCheckPoints;
import org.sippr.service.SdCheckPointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/sd/cp")
public class SdCheckPointsController {
    @Autowired
    private SdCheckPointsService sdCheckPointsService;

    @Value("${file.upload.path}")
    private String baseDirectory;

    @PostMapping
    public Result<String> add(@RequestBody @Validated SdCheckPoints value) {
        SdCheckPoints sdCheckPoints = sdCheckPointsService.findByName(value.getName());
        if (sdCheckPoints != null) {
            return Result.error("名称已存在");
        }
        sdCheckPoints = sdCheckPointsService.findByAlias(value.getAlias());
        if (sdCheckPoints != null) {
            return Result.error("别名已存在");
        }
        sdCheckPointsService.add(value);
        return Result.success();
    }

    @PutMapping
    public Result<String> update(@RequestBody @Validated(SdCheckPoints.Update.class) SdCheckPoints value) {
//        SdCheckPoints sdCheckPoints = sdCheckPointsService.findByName(value.getName());
//        if (sdCheckPoints != null) {
//            return Result.error("名称已存在");
//        }
        sdCheckPointsService.update(value);
        return Result.success();
    }


    //上传sd路径下的checkpoints模型图标，同时解析名字，生成大模型记录数据
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        String oFile = file.getOriginalFilename();
        if (oFile == null)
            return Result.error("文件为空！");
        String cpName = oFile.replace(".preview.png", "");

        //判断此大模型，是否已经存在
        SdCheckPoints old = sdCheckPointsService.findByAlias(cpName);
        if (old != null) {
            return Result.error("大模型已存在！");
        }

        String filename = UUID.randomUUID().toString() + oFile.substring(oFile.lastIndexOf("."));
        Path dir = Paths.get(baseDirectory, "\\check_points");
        if (!Files.exists(dir)) {
            Files.createDirectories(dir);
        }

        Path filePath = dir.resolve(filename);
        file.transferTo(new File(filePath.toString()));


        //初始化类实例
        SdCheckPoints sdCheckPoints = new SdCheckPoints();
        sdCheckPoints.setName(cpName);
        sdCheckPoints.setAlias(cpName);
        sdCheckPoints.setImg(filename);

        sdCheckPointsService.add(sdCheckPoints);


        return Result.success(filename);
    }


    @GetMapping("list")
    public Result<List<SdCheckPoints>> list() {
        List<SdCheckPoints> sdCheckPoints = sdCheckPointsService.list();
        for (SdCheckPoints cp : sdCheckPoints) {
            cp.setImg("/file/check_points/" + cp.getImg());
        }
        return Result.success(sdCheckPoints);
    }

    @DeleteMapping
    public Result<String> delete(Integer id) {
        sdCheckPointsService.delete(id);
        return Result.success();
    }

}
